package com.shd.edanpei.customer.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.ui.activity.SureLoanInfoActivity;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.base.rxbus.Subscribe;
import com.shd.edanpei.bean.OrderDetailsModel;
import com.shd.edanpei.bean.StatusBean;
import com.shd.edanpei.presenter.contract.OrderDetailsContract;
import com.shd.edanpei.enums.StatusEnum;
import com.shd.edanpei.presenter.OrderDetailsPresenter;
import com.shd.edanpei.ui.view.AuditDataView;
import com.shd.edanpei.ui.view.MeCustomerView;
import com.shd.edanpei.ui.view.MeIntegralView;
import com.shd.edanpei.ui.view.MeOrderStatusView;
import com.shd.edanpei.ui.view.OrderInfoView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/9/27.
 * yang-wen-wen
 */

public class COrderDetailsActivity extends BaseActivity<OrderDetailsPresenter> implements OrderDetailsContract.OrderDetailsIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.container)
    LinearLayout container;
    @BindView(R.id.tv_pay)
    TextView tvPay;
    @BindView(R.id.tv_payService)
    TextView tvPayService;
    @BindView(R.id.tv_sure)
    TextView tvSure;


    private OrderDetailsModel model;
    private int index;
    private int orderID;
    private Bundle bundle;


    @Override
    protected int setLayout() {
        return R.layout.activity_corderdetails;
    }

    @Override
    protected OrderDetailsPresenter bindPresenter() {
        return new OrderDetailsPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        RxBus.get().register(this);
        headline.setText("我的订单");

        bundle = getIntent().getExtras();
        index = bundle.getInt(Constant.INDEX);
        orderID = bundle.getInt(Constant.ORDER_ID);
        mPresenter.requetData(orderID);
    }

    @Override
    protected void initListener() {
        tvPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt(Constant.INDEX, index);
                bundle.putInt(Constant.ORDER_ID, model.getBaseInfo().getOrderId());
                bundle.putInt(Constant.TYPE, model.getBaseInfo().getUserStatus());
//                startActivity(PayActivity.class, bundle);

                mPresenter.checkAuthorization(model,bundle);

            }
        });
        tvPayService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt(Constant.INDEX, index);
                bundle.putInt(Constant.ORDER_ID, model.getBaseInfo().getOrderId());
                bundle.putInt(Constant.TYPE, model.getBaseInfo().getUserStatus());
                startActivity(SureLoanInfoActivity.class, bundle);
            }
        });
        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putInt(Constant.INDEX, index);
                bundle.putInt(Constant.ORDER_ID, model.getBaseInfo().getOrderId());
                bundle.putInt(Constant.TYPE, model.getBaseInfo().getUserStatus());
                mPresenter.sureGetLone(bundle);
            }
        });
    }

    @OnClick(R.id.iv_headline_back)
    void onClose() {
        finish();
    }

    @Override
    public void setData(OrderDetailsModel model) {
        this.model = model;
        container.addView(new MeOrderStatusView(this, model));
        container.addView(new AuditDataView(this, model, Constant.ME_ORDER));
        container.addView(new MeCustomerView(this, model));
        if(StatusEnum.getCDes(model.getBaseInfo().getUserStatus()).equals("已完成"))
            container.addView(new MeIntegralView(this,model));
        container.addView(new OrderInfoView(this, model));

        if (model.getBaseInfo().getUserStatus() == StatusEnum.unpaid.getStatus()) {
            tvPay.setVisibility(View.VISIBLE);
        } else if (model.getBaseInfo().getUserStatus() == StatusEnum.waitService.getStatus()) {
            tvPayService.setVisibility(View.VISIBLE);
        } else {

        }
    }


    //刷新列表
    @Subscribe(code = RxBusCode.REFRESH_ORDER_STATUS)
    public void refreshStatus(StatusBean bean) {
        if (bean.getOrderStatus() == StatusEnum.applying.getStatus()) {
            //显示退款流程
            tvPay.setVisibility(View.GONE);
        } else if (bean.getOrderStatus() == StatusEnum.waitLoan.getStatus()) {
            tvPay.setVisibility(View.GONE);
            tvPayService.setVisibility(View.GONE);
        }
    }


    @Override
    protected void onDestroy() {
        container.removeAllViews();
        super.onDestroy();
    }
}
